package com.gzy.cbrsystem.shimian.dao;

import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

/**
 * A data access object (DAO) providing persistence and search support for
 * CaseBasicInfo entities. Transaction control of the save(), update() and
 * delete() operations can directly support Spring container-managed
 * transactions or they can be augmented to handle user-managed Spring
 * transactions. Each of these methods provides additional information for how
 * to configure it for the desired type of transaction control.
 * 
 * @see com.gzy.cbrsystem.shimian.dao.CaseBasicInfo
 * @author MyEclipse Persistence Tools
 */

public class CaseBasicInfoDAO extends HibernateDaoSupport {
	private static final Log log = LogFactory.getLog(CaseBasicInfoDAO.class);
	// property constants
	public static final String PATIENT_ID = "patientId";
	public static final String PATIENT_NAME = "patientName";
	public static final String PATIENT_PHONE = "patientPhone";
	public static final String INVESTIGATOR = "investigator";
	public static final String REGISTRAR = "registrar";
	public static final String CHECKER = "checker";
	public static final String DOCTOR_NAME = "doctorName";
	public static final String CATEGORY = "category";
	public static final String LOG_TIME = "logTime";

	protected void initDao() {
		// do nothing
	}

	public void save(CaseBasicInfo transientInstance) {
		log.debug("saving CaseBasicInfo instance");
		try {
			getHibernateTemplate().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(CaseBasicInfo persistentInstance) {
		log.debug("deleting CaseBasicInfo instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public CaseBasicInfo findById(java.lang.String id) {
		log.debug("getting CaseBasicInfo instance with id: " + id);
		try {
			CaseBasicInfo instance = (CaseBasicInfo) getHibernateTemplate()
					.get("com.gzy.cbrsystem.shimian.dao.CaseBasicInfo", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(CaseBasicInfo instance) {
		log.debug("finding CaseBasicInfo instance by example");
		try {
			List results = getHibernateTemplate().findByExample(instance);
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding CaseBasicInfo instance with property: "
				+ propertyName + ", value: " + value);
		try {
			String queryString = "from CaseBasicInfo as model where model."
					+ propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByPatientId(Object patientId) {
		return findByProperty(PATIENT_ID, patientId);
	}

	public List findByPatientName(Object patientName) {
		return findByProperty(PATIENT_NAME, patientName);
	}

	public List findByPatientPhone(Object patientPhone) {
		return findByProperty(PATIENT_PHONE, patientPhone);
	}

	public List findByInvestigator(Object investigator) {
		return findByProperty(INVESTIGATOR, investigator);
	}

	public List findByRegistrar(Object registrar) {
		return findByProperty(REGISTRAR, registrar);
	}

	public List findByChecker(Object checker) {
		return findByProperty(CHECKER, checker);
	}

	public List findByDoctorName(Object doctorName) {
		return findByProperty(DOCTOR_NAME, doctorName);
	}

	public List findByCategory(Object category) {
		return findByProperty(CATEGORY, category);
	}

	public List findByLogTime(Object logTime) {
		return findByProperty(LOG_TIME, logTime);
	}

	public List findAll() {
		log.debug("finding all CaseBasicInfo instances");
		try {
			String queryString = "from CaseBasicInfo";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public CaseBasicInfo merge(CaseBasicInfo detachedInstance) {
		log.debug("merging CaseBasicInfo instance");
		try {
			CaseBasicInfo result = (CaseBasicInfo) getHibernateTemplate()
					.merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(CaseBasicInfo instance) {
		log.debug("attaching dirty CaseBasicInfo instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(CaseBasicInfo instance) {
		log.debug("attaching clean CaseBasicInfo instance");
		try {
			getHibernateTemplate().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public static CaseBasicInfoDAO getFromApplicationContext(
			ApplicationContext ctx) {
		return (CaseBasicInfoDAO) ctx.getBean("CaseBasicInfoDAO");
	}
}